package com.wxy.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.security.core.context.SecurityContextHolder;

public class SecurityUtil {

    private static JSONObject getUserDetails() {
        try {
            String json = JSON.toJSONString(SecurityContextHolder.getContext().getAuthentication().getPrincipal());
            JSONObject jwtUser = JSON.parseObject(json);
            return jwtUser;
        }catch (Exception e){
            return  null;
        }
    }

    public static String getUsername() {
        JSONObject jwtUser = getUserDetails();
        if (jwtUser != null){
            return jwtUser.getString("username");
        }
        return "";
    }

    public static Long getUserId() {
        JSONObject jwtUser = getUserDetails();
        if (jwtUser != null){
            return jwtUser.getLong("id");
        }
        return 0L;
    }

    public static Integer getPrivilegeType() {
        JSONObject jwtUser = getUserDetails();
        if (jwtUser != null){
            return jwtUser.getInteger("privilegeType");
        }
        return 0;
    }

    public static Long getPrivilegeValue() {
        JSONObject jwtUser = getUserDetails();
        if (jwtUser != null){
            return jwtUser.getLong("privilegeValue");
        }
        return 0L;
    }

}
